// 3个状态 fufilled pending rejected
const PENDING = 'PENDING';
const FULFILLED = 'FULFILLED';
const REJECTED = 'REJECTED';

class MyPromise{
    constructor(executor){
        this.status = PENDING;// 初始的状态
        this.reason = undefined;
        this.value = undefined;
        let resolve = value=>{
            if(this.status==PENDING){
                this.value = value;
                this.status = FULFILLED
            }
        }

        let reject = reason=>{
            if(this.status == PENDING){
                this.reason = reason;
                this.status = REJECTED;
            }
        }

        try {
            executor(resolve,reject)
        } catch (error) {
            reject(error)
        }
    }

    then(onFufilled,onRejected){
        if(this.status==FULFILLED){
            onFufilled(this.value);
        }
        if(this.status == REJECTED){
            onRejected(this.reason)
        }
    }
}

new MyPromise((resolve,reject)=>{
    reject('失败啦');
}).then(
    res=>console.log('成功',res),
    err=>console.log('失败',err)
)